import time
import threading
import random

def product(tasks,i=0):
    while 1:
        i += 1
        task = 'task_id{}'.format(i)
        time.sleep(random.randint(1,3))
        tasks.append(task)
        print('添加任务{}成功!'.format(i))

def consumer(tasks):
    while 1:
        time.sleep(random.randint(3, 4))
        task = tasks.pop()
        print('完成任务编号: {} 成功!'.format(task))


if __name__ == '__main__':
    tasks = []
    i = 0
    t1 = threading.Thread(target=product,args=(tasks,i))
    t2 = threading.Thread(target=consumer,args=(tasks,))

    t1.start()
    t2.start()

    t1.join()
    t2.join()

    print('任务执行完毕')